package com.jtx.miaoah2.Service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jtx.miaoah2.Cache.GoodsSeckillCache;
import com.jtx.miaoah2.Common.BaseResponse;
import com.jtx.miaoah2.Common.CustomException;
import com.jtx.miaoah2.Mapper.SkGoodsMapper;
import com.jtx.miaoah2.Mapper.SkGoodsSeckillMapper;
import com.jtx.miaoah2.Model.goods.DO.SkGoods;
import com.jtx.miaoah2.Model.goods.DO.SkGoodsSeckill;
import com.jtx.miaoah2.Service.SkGoodsSeckillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author 季台星
 * @Date 2023 07 23 16 53
 */
@Service
@Slf4j
public class SkGoodsSeckillServiceImpl extends ServiceImpl<SkGoodsSeckillMapper, SkGoodsSeckill> implements SkGoodsSeckillService {
    @Autowired
    private SkGoodsSeckillMapper skGoodsSeckillMapper;

    @Autowired
    private GoodsSeckillCache goodsSeckillCache;

    @Override
    public Object createSnapped(SkGoodsSeckill skGoodsSeckill) {
        //创建订单,校验参数
        if (skGoodsSeckill.getGoodsId() == null){
            throw new CustomException("创建秒杀任务出现异常，商品为空");
        }
        if (null == skGoodsSeckill.getStockCount() || skGoodsSeckill.getStockCount() <= 0){
            throw new CustomException("库存不得为空");
        }
        //校验开始和结束时间
        if (null == skGoodsSeckill.getStartDate() || null == skGoodsSeckill.getEndDate()){
            //默认开始时间为2分钟后，持续时间为10分钟
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MINUTE, 2);
            skGoodsSeckill.setStartDate(calendar.getTime());
            calendar.add(Calendar.MINUTE, 10);
            skGoodsSeckill.setEndDate(calendar.getTime());
        }
        if (skGoodsSeckill.getStartDate().before(new Date())){
            throw new CustomException("开始时间不能小于当前时间");
        }
        try {
            skGoodsSeckillMapper.insert(skGoodsSeckill);
            goodsSeckillCache.addSeckill(skGoodsSeckill);
        } catch (Exception e) {
            log.error("创建秒杀任务出现异常 ： {}" + e.getMessage());
            e.printStackTrace();
            throw new CustomException("创建秒杀任务失败，请联系管理员");
        }
        //数据预热，创建商品数量相当的token到数据库中
        goodsSeckillCache.addToken(skGoodsSeckill);
        //保存任务
        return BaseResponse.success("秒杀任务创建成功");
    }

    @Override
    public BaseResponse getSnappedList(Integer pageNo, Integer pageSize) {
        IPage<SkGoodsSeckill> seckillList = goodsSeckillCache.getSeckillList(pageNo, pageSize);
        if (!Objects.isNull(seckillList)){
            return BaseResponse.success(seckillList);
        }
        /**
         * 从数据库中查询
         */
        IPage<SkGoodsSeckill> skGoodsSeckillIPage = new Page<>(pageNo,pageSize);
        seckillList = skGoodsSeckillMapper.selectPage(skGoodsSeckillIPage,null);
        goodsSeckillCache.addSeckillList(seckillList, pageNo, pageSize);
        return BaseResponse.success(seckillList);
    }

    @Override
    public BaseResponse getSnappedById(Long id) {
        //从缓存中查询
        SkGoodsSeckill seckill = goodsSeckillCache.getSeckill(id);
        if (Objects.isNull(seckill)){
            seckill = skGoodsSeckillMapper.selectById(id);
            goodsSeckillCache.addSeckill(seckill);
            if (null == seckill){
                throw new CustomException("秒杀活动不存在！！！");
            }
        }
        return BaseResponse.success(seckill);
    }
}
